package com.canis.stockmanager.utils;

import net.bytebuddy.TypeCache;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.List;

public class util {
    public static Pageable getPageable(String sortStr,Integer start,Integer limit){
        String sort_c="id";
        boolean desc=true;
        if(sortStr.length()>0){
            String sorts[]=sortStr.split("\"");
            if(sorts.length>8){
                sort_c= sorts[3];
                if(sorts[7].equals("DESC")){
                    desc = true;
                }else{
                    desc=false;
                }
            }
        }
        Sort sort = Sort.by(desc?Sort.Direction.DESC:Sort.Direction.ASC, sort_c);
        Pageable pageable = PageRequest.of(start/limit, limit,sort);
        return pageable;
    }
    public static Pageable getPageable2(String sort_c,Integer start,Integer limit){
        Sort sort = Sort.by(Sort.Direction.DESC, sort_c);
        Pageable pageable = PageRequest.of(start/limit, limit,sort);
        return pageable;
    }
}
